from django.db import models
from django.contrib.auth.models import AbstractUser
from django.forms import ValidationError
import re

# Create your models here.
# 用户模型类
class User(AbstractUser):
    phone = models.CharField("手机号",max_length=11, unique=True, null=True, blank=True)
    last_login = models.DateTimeField("上次登录", null=True, blank=True)

    # 局部校验器
    def validated_username(self,username):
        if re.findall(r'^[A-Z]$',username):
            return
        else:
            raise ValidationError('职责名称不匹配')

    def __str__(self):
        return self.username
    class Meta:
        db_table = 'user_tb'
        verbose_name_plural = '用户表'


# 用户图片模型类
class UserImage(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    avatar = models.CharField('用户头像',max_length=300)

    def __str__(self):
        return self.user.username

    class Meta:
        db_table = 'user_image'
        verbose_name_plural = '用户头像'
